/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.CustomerDao;
import com.chlitina.store.modules.base.entity.Customer;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;


/**
 * 客户Service
 * @author lvbo
 * @version 2013-07-03
 */
@Component
@Transactional(readOnly = true)
public class CustomerService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CustomerService.class);
	
	@Autowired
	private CustomerDao customerDao;
	
	public Customer get(Long id) {
		return customerDao.findOne(id);
	}
	public Page<Customer> find(Page<Customer> page, String type, String keyWord) {
		//设置页面显示个数
		page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
		DetachedCriteria dc = customerDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (keyWord == null) {
			keyWord = "";
		}
		if (type == null || "0".equals(type)) {
			dc.add(Restrictions.like("name", "%"+keyWord+"%"));
		} else if ("1".equals(type)) {
			dc.add(Restrictions.like("nickname", "%"+keyWord+"%"));
		} else if ("2".equals(type)) {
			dc.add(Restrictions.like("mobile", "%"+keyWord+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return customerDao.find(page, dc);
	}
	public List<Customer> export(String type, String keyWord) {
		customerDao.clear();
		DetachedCriteria dc = customerDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (keyWord == null) {
			keyWord = "";
		}
		if (type == null || "0".equals(type)) {
			dc.add(Restrictions.like("name", "%"+keyWord+"%"));
		} else if ("1".equals(type)) {
			dc.add(Restrictions.like("nickname", "%"+keyWord+"%"));
		} else if ("2".equals(type)) {
			dc.add(Restrictions.like("mobile", "%"+keyWord+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return customerDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public List<Customer> isUnique(String mobile) {
		DetachedCriteria dc = customerDao.createDetachedCriteria();
		dc.add(Restrictions.eq("mobile", mobile));
		return customerDao.find(dc);
	}
	
	
	@Transactional(readOnly = false)
	public void save(Customer customer) {
		customerDao.clear();
		customerDao.save(customer);
	//	customerDao.refresh(customer);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		customerDao.delete(customerDao.findOne(id));
	}
	
	public List<Customer> getByStore() {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = customerDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", store));
		return customerDao.find(dc);
	}
}
